/**
 * Created by liyanqinqin2 on 2023/3/17.
 */
function Change_second_selectwords() {
    var result = "";
    //根据id找到两个下拉框对象
    var target1 = document.getElementById("dataSourceId");
    var selected_initial = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("tableName");
    //得到第一个下拉框的内容
    // 请求的 URL 地址
    var url = '/metadata/metacolumn/getTableList'
    // 请求的参数对象
    var paramsObj = {sourceId: selected_initial}
    // 调用 axios.get() 发起 GET 请求
    axios.get(url, { params: paramsObj }).then(function(res) {
        // res.data 是服务器返回的数据
        result = res.data
        var selected_initial = target1.options[target1.selectedIndex].value;

        //清空第二个下拉框
        while (target2.options.length) {
            target2.remove(0);
        }
        //根据第一个下拉框的内容找到对应的列表
        target2.options.add(new Option("", ""))
        if (result.length>0) {
            for (var i = 0; i < result.length; i++) {
                var item = new Option(result[i], result[i]);
                //将列表中的内容加入到第二个下拉框
                target2.options.add(item);
            }
        }
        console.log(res)
    })
    Change_third_selectwords();
};

function Change_third_selectwords() {
    //根据id找到两个下拉框对象
    var target1 = document.getElementById("dataSourceId");
    var data_source_id = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("tableName");
    var table_id = target2.options[target2.selectedIndex].value;
    var target3 = document.getElementById("featureCols");
    var target4 = document.getElementById("classCols");
    //得到第一个下拉框的内容
    // 请求的 URL 地址
    var url = '/metadata/metacolumn/getTableColumns'
    // 请求的参数对象
    var paramsObj = {sourceId: data_source_id, tableId: table_id}
    // 调用 axios.get() 发起 GET 请求
    axios.get(url, { params: paramsObj }).then(function(res) {
        // res.data 是服务器返回的数据
        col_list_result = res.data
        var selected_initial = target1.options[target1.selectedIndex].value;

        //清空第二个下拉框
//            while (target3.options.length) {
//                target3.remove(0);
//            }
        //根据第一个下拉框的内容找到对应的列表
        var feature_col_input = ""
        if (col_list_result.length>0) {
            for (var i = 0; i < col_list_result.length; i++) {
                //将列表中的内容加入到第二个下拉框
                feature_col_input += "<input type='checkbox' name='featureCols' value='"+col_list_result[i].columnName+"'>"+col_list_result[i].columnName+"&emsp;"+"</input>"
            }
        }
        var class_col_input = ""
        if (col_list_result.length>0) {
            for (var i = 0; i < col_list_result.length; i++) {
                //将列表中的内容加入到第二个下拉框
                class_col_input += "<input type='checkbox' name='classCols' value='"+col_list_result[i].columnName+"'>"+col_list_result[i].columnName+"&emsp;"+"</input>"
            }
        }
        target3.innerHTML = feature_col_input
        target4.innerHTML = class_col_input
        console.log(res)
    })
};

function display(id){
    var traget=document.getElementById(id);
    if(traget.style.display=="none"){
        traget.style.display="";
    }else{
        traget.style.display="none";
    }
};

function display_pre_process(id){
    var traget=document.getElementById(id);
    if(traget.style.display=="none"){
        traget.style.display="";
        //填充特征变量
        var target = document.getElementById("preProcessFeature");
        //根据第一个下拉框的内容找到对应的列表
        target.options.add(new Option("", ""))
        if (col_list_result.length>0) {
            for (var i = 0; i < col_list_result.length; i++) {
                var item = new Option(col_list_result[i].columnName, col_list_result[i].columnName);
                //将列表中的内容加入到第二个下拉框
                target.options.add(item);
            }
        }
    }else{
        traget.style.display="none";
    }
};

function Change_pre_process_method(el) {
    el_parent = el.parentNode
    var div_id = el_parent.getAttribute("id")
    var index = div_id.substr(-1)
    var target1 = document.getElementById("preProcessMethod" + index);
    var selected_initial = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("preProcessMethodValue" + index);
    //清空第二个下拉框
    while (target2.options.length) {
        target2.remove(0);
    }
    if (selected_initial == "fillna") {
        var fillna_result = [{"k":"mean", "v":"均值"}, {"k":"median", "v":"中位数"}, {"k":"most_frequent", "v":"众数"}, {"k":"constant_0","v":"常数(0)"},{"k":"constant_1","v":"常数(1)"}]
        //根据第一个下拉框的内容找到对应的列表
        if (fillna_result.length>0) {
            for (var i = 0; i < fillna_result.length; i++) {
                var item = new Option(fillna_result[i].v, fillna_result[i].k);
                //将列表中的内容加入到第二个下拉框
                target2.options.add(item);
            }
        }
    } else if (selected_initial == "transType") {
        var type_result = [{"k":"int", "v":"int"}, {"k":"float", "v":"float"}]
        //根据第一个下拉框的内容找到对应的列表
        if (type_result.length>0) {
            for (var i = 0; i < type_result.length; i++) {
                var item = new Option(type_result[i].v, type_result[i].k);
                //将列表中的内容加入到第二个下拉框
                target2.options.add(item);
            }
        }
    }
};

function remove_pre_process_row(el) {
    tbEM.removeChild(el.parentNode);
    index--;
};

function add_pre_process_row() {
    var tbEM = document.querySelector('#tbEM');
    var div = document.createElement('div');
//        div.innerHTML = '收件邮箱: <input type="text"/> <input type="button" value="+" class="button" onclick="add()" /><input type="button" value="-" class="button" onclick="remove(this)" />';
    preProcessRow = document.getElementById("preProcessRow1")
    preProcessRow = preProcessRow.innerHTML
    preProcessRow = preProcessRow.replace("preProcessMethod1", "preProcessMethod" + index)
    preProcessRow = preProcessRow.replace("preProcessMethodValue1", "preProcessMethodValue" + index)
    preProcessRow = preProcessRow.replace("preProcessMethodList[0].preProcessFeature", "preProcessMethodList["+index+"].preProcessFeature")
    preProcessRow = preProcessRow.replace("preProcessMethodList[0].preProcessMethod", "preProcessMethodList["+index+"].preProcessMethod")
    preProcessRow = preProcessRow.replace("preProcessMethodList[0].preProcessMethodValue", "preProcessMethodList["+index+"].preProcessMethodValue")
    div.innerHTML = preProcessRow + '<input type="button" value="-" class="button" onclick="remove_pre_process_row(this)" />'
    div.id="preProcessRow" + index
    div.style = "position:relative;left:5px;bottom: 5px"
    div.className = "preProcessRow"
    tbEM.appendChild(div);
    // $("#preProcessRow" + index).addClass("preProcessRow")
    index++;
};

function pop_up_window() {
    var target1 = document.getElementById("dataSourceId");
    var data_source_id = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("tableName");
    var table_id = target2.options[target2.selectedIndex].value;
    window.open("/algo/previewdata/addpage?dataSourceId="+data_source_id+"&tableName="+table_id,
        "数据预览",
        height=200,
        innerHeight=200,
        width=200,
        innerWidth=200,
        top=200)
};



function Change_second_selectwords_kmeans() {
    var result = "";
    //根据id找到两个下拉框对象
    var target1 = document.getElementById("dataSourceId");
    var selected_initial = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("tableName");
    //得到第一个下拉框的内容
    // 请求的 URL 地址
    var url = '/metadata/metacolumn/getTableList'
    // 请求的参数对象
    var paramsObj = {sourceId: selected_initial}
    // 调用 axios.get() 发起 GET 请求
    axios.get(url, { params: paramsObj }).then(function(res) {
        // res.data 是服务器返回的数据
        result = res.data
        var selected_initial = target1.options[target1.selectedIndex].value;

        //清空第二个下拉框
        while (target2.options.length) {
            target2.remove(0);
        }
        //根据第一个下拉框的内容找到对应的列表
        target2.options.add(new Option("", ""))
        if (result.length>0) {
            for (var i = 0; i < result.length; i++) {
                var item = new Option(result[i], result[i]);
                //将列表中的内容加入到第二个下拉框
                target2.options.add(item);
            }
        }
        console.log(res)
    })
    Change_third_selectwords_kmeans();
};

function Change_third_selectwords_kmeans() {
    //根据id找到两个下拉框对象
    var target1 = document.getElementById("dataSourceId");
    var data_source_id = target1.options[target1.selectedIndex].value;
    var target2 = document.getElementById("tableName");
    var table_id = target2.options[target2.selectedIndex].value;
    var target3 = document.getElementById("featureCols");
    //得到第一个下拉框的内容
    // 请求的 URL 地址
    var url = '/metadata/metacolumn/getTableColumns'
    // 请求的参数对象
    var paramsObj = {sourceId: data_source_id, tableId: table_id}
    // 调用 axios.get() 发起 GET 请求
    axios.get(url, { params: paramsObj }).then(function(res) {
        // res.data 是服务器返回的数据
        col_list_result = res.data
        var selected_initial = target1.options[target1.selectedIndex].value;

        //清空第二个下拉框
//            while (target3.options.length) {
//                target3.remove(0);
//            }
        //根据第一个下拉框的内容找到对应的列表
        var feature_col_input = ""
        if (col_list_result.length>0) {
            for (var i = 0; i < col_list_result.length; i++) {
                //将列表中的内容加入到第二个下拉框
                feature_col_input += "<input type='checkbox' name='featureCols' value='"+col_list_result[i].columnName+"'>"+col_list_result[i].columnName+"&emsp;"+"</input>"
            }
        }
        target3.innerHTML = feature_col_input
        console.log(res)
    })
};
